home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / PowerPlant / CURLPushButton 2.3 / CURLPushButton Test 68k / IC / CInternetConfig.cpp next >
Encoding:
C/C++ Source or Header  |  1997-06-22  |  8.9 KB  |  402 lines  |  [TEXT/CWIE]

  1. /***********************************************************************\
  2.     CInternetConfig.h
  3.     
  4.     A C++ class for using Internet Config
  5.     
  6.     by Dan Crevier
  7.     
  8.     version 1.1
  9.     
  10.     Some additions by Jerry Aman and Tom Emerson
  11.  
  12. \***********************************************************************/
  13.  
  14. #include "CInternetConfig.h"
  15.  
  16. #ifndef __ICAPI__
  17. #include "ICAPI.h"
  18. #endif
  19.  
  20. #ifndef __COMPONENTS__
  21. #include <Components.h>
  22. #endif
  23.  
  24. #ifndef __GESTALT__
  25. #include <Gestalt.h>
  26. #endif
  27.  
  28. static void ClearMem(void *start, long len);
  29.  
  30. /***********************************************************************\
  31.     CInternetConfig - constructor
  32.         If prefFile is NULL, the default preferences are used
  33. \***********************************************************************/
  34.  
  35. CInternetConfig::CInternetConfig(OSType appSig)
  36. : started(false), mappings(0), applicationSig(appSig), installed(false)
  37. {
  38.     ComponentDescription ICDescription = {'PREF', 'ICAp', 0L, 0L, 0L};
  39.     long response;
  40.  
  41.     // see if component version of IC is installed
  42.     if (Gestalt(gestaltComponentMgr,&response)==noErr)
  43.     {
  44.         // first, see if IC is installed
  45.         if (CountComponents(&ICDescription))
  46.         {
  47.             installed = true;
  48.         }
  49.     }
  50. }
  51.         
  52. /***********************************************************************\
  53.     ~CInternetConfig - destructor
  54. \***********************************************************************/
  55.  
  56. CInternetConfig::~CInternetConfig()
  57. {
  58.     Stop();
  59. }
  60.  
  61. /***********************************************************************\
  62.     Start - starts up IC
  63. \***********************************************************************/
  64.  
  65. ICError CInternetConfig::Start(FSSpec *prefFile)
  66. {
  67.     ICError err;
  68.     
  69.     err = ICStart(&inst, applicationSig); 
  70.     if (err == noErr)
  71.     {
  72.         if (prefFile == NULL) // use default prefs
  73.         {
  74.             err = ICFindConfigFile(inst, 0, NULL);
  75.         }
  76.         else
  77.         {
  78.             ICDirSpec prefDir;
  79.             
  80.             prefDir.vRefNum = prefFile->vRefNum;
  81.             prefDir.dirID = prefFile->parID;
  82.             err = ICFindUserConfigFile(inst, &prefDir);
  83.         }
  84.         if (err == noErr) // startup was successful
  85.         {
  86.              started = true;
  87.         }
  88.         else
  89.         {
  90.             // if ICStart was called, but pref couldn't be found, we
  91.             // need to call ICStop
  92.  
  93.             ICStop(inst);
  94.         }
  95.     }
  96.     return err;
  97. }
  98.  
  99. /***********************************************************************\
  100.     Stop - stop the instance
  101. \***********************************************************************/
  102.  
  103. ICError CInternetConfig::Stop()
  104. {
  105.     if (started)
  106.     {
  107.         started = false;
  108.         
  109.         return ICStop(inst);
  110.     }
  111.     return noErr;
  112. }
  113.  
  114. /***********************************************************************\
  115.     DoURL - launch the URL past in
  116. \***********************************************************************/
  117.  
  118. ICError CInternetConfig::DoURL(StringPtr theURL)
  119. {
  120.     long start = 0, end;
  121.     
  122.     if (started)
  123.     {
  124.         end = theURL[0];
  125.         return ICLaunchURL(inst, "\p", (char *)theURL+1, end, &start, &end);
  126.     }
  127.     else
  128.     {
  129.         return -1;
  130.     }
  131. }
  132.  
  133. /***********************************************************************\
  134.     MapFilename - return the map entry for the given filename
  135. \***********************************************************************/
  136.  
  137. ICError CInternetConfig::MapFilename(StringPtr name, ICMapEntry *entry)
  138. {
  139.     if (started)
  140.     {
  141.         return ICMapFilename(inst, name, entry);
  142.     }
  143.     else
  144.     {
  145.         return -1;
  146.     }
  147. }
  148.  
  149. /***********************************************************************\
  150.     MapFilename - return the map entry for the given filename
  151.         takes name as C string
  152. \***********************************************************************/
  153.  
  154. ICError CInternetConfig::MapFilename(char *name, ICMapEntry *entry)
  155. {
  156.     short i;
  157.     Str255 newName;
  158.     
  159.     // convert name to P string
  160.     for(i=0; name[i] && i<255; i++)
  161.     {
  162.         newName[i+1] = name[i];
  163.     }
  164.     newName[0] = i;
  165.     
  166.     return MapFilename(newName, entry);
  167. }
  168.  
  169. /***********************************************************************\
  170.     StartMapIteration -- starts iterating over the mappings
  171.         Note: It calls ICBegin() and FinishMapIteration calls
  172.             ICEnd(), so you have can't handle events in between
  173. \***********************************************************************/
  174.  
  175. ICError CInternetConfig::StartMapIteration(void)
  176. {
  177.     ICError err;
  178.     ICAttr            attr = 0;
  179.     
  180.     currentPos = 1;
  181.  
  182.     if (!started) return -1;
  183.     
  184.     err = ICBegin(inst, icReadOnlyPerm);
  185.     if (err != noErr) return err;
  186.     
  187.     err = ICGetPrefHandle(inst, kICMapping, &attr, &mappings);
  188.     
  189.     if (!err)
  190.     {
  191.         err = ICCountMapEntries(inst, mappings, &numMappings);
  192.     }
  193.     
  194.     if (err)
  195.     {
  196.         ICEnd(inst);
  197.     }
  198.     
  199.     return err;
  200. }
  201.  
  202. /***********************************************************************\
  203.     NextMapEntry - get the next map entry after calling
  204.         StartMapIteration.  Returns false if there are no more entries    
  205. \***********************************************************************/
  206.  
  207. Boolean CInternetConfig::NextMapEntry(ICMapEntry *entry)
  208. {
  209.     long pos;
  210.  
  211.     if (!started || mappings == NULL) return false;
  212.     
  213.     // are we done?
  214.     if (currentPos > numMappings) return false;
  215.     
  216.     ICGetIndMapEntry(inst, mappings, currentPos, &pos, entry);
  217.  
  218.     currentPos++;
  219.     
  220.     return true;
  221. }
  222.  
  223. /***********************************************************************\
  224.     FinishMapIteration -- call after doing a map iteration    
  225. \***********************************************************************/
  226.  
  227. ICError CInternetConfig::FinishMapIteration(void)
  228. {
  229.     if (!started || mappings == NULL) return -1;
  230.     
  231.     DisposeHandle(mappings);
  232.     mappings = NULL;
  233.     
  234.     return ICEnd(inst);
  235. }
  236.  
  237. /***********************************************************************\
  238.     GetFontRecord
  239.     Return ICFontRecord for specified key - key names are in "ICKeys.h"
  240. \***********************************************************************/
  241.  
  242. ICError CInternetConfig::GetFontRecord(ConstStr255Param keyStr, ICFontRecord
  243.     *fontRecord)
  244. {
  245.     ICAttr junkAttr;
  246.     long size = sizeof(ICFontRecord);
  247.     
  248.     if (started)
  249.     {
  250.         return ICGetPref(inst, keyStr, &junkAttr, (char *)fontRecord, &size);
  251.     }
  252.     else
  253.     {
  254.         return -1;
  255.     }
  256. };
  257.  
  258.  
  259.  
  260. /***********************************************************************\
  261.     GetKeysString
  262.     Return Pascal string for specified key - key names are in "ICKeys.h"
  263.     By Jerry Aman
  264. \***********************************************************************/
  265.  
  266. ICError CInternetConfig::GetKeysString(ConstStr255Param keyStr, Str255
  267.             thePStr)
  268. {
  269.     ICAttr junkAttr;
  270.     long size = 256;
  271.     
  272.     if (started)
  273.     {
  274.         return ICGetPref(inst, keyStr, &junkAttr, (char *)thePStr, &size);
  275.     }
  276.     else
  277.     {
  278.         return -1;
  279.     }
  280. };
  281.  
  282.  
  283. /***********************************************************************\
  284.     EditPreferences
  285.     No need to supply a key string to edit the default settings
  286.     By Jerry Aman
  287. \***********************************************************************/
  288.  
  289. ICError CInternetConfig::EditPreferences(ConstStr255Param key)
  290. {
  291.     if (started)
  292.     {
  293.         return ICEditPreferences(inst, key);
  294.     }
  295.     else
  296.     {
  297.         return -1;
  298.     }
  299. }
  300.  
  301. /***********************************************************************\
  302.     GetHelperFileSpec -- return the FSSpec to the latest incarnation
  303.         of the specified helper application
  304.     By Tom Emerson
  305. \***********************************************************************/
  306.  
  307. ICError CInternetConfig::GetHelperFileSpec(StringPtr key, FSSpec *theFileSpec,
  308.     OSType *creator)
  309. {
  310.     ICAttr        junkAttr;
  311.     ICAppSpec    appSpec;
  312.     long        size = sizeof(ICAppSpec);
  313.     
  314.     if (started)
  315.     {
  316.         if (ICGetPref(inst, key, &junkAttr, (char *)&appSpec, &size) == noErr)
  317.         {
  318.             if (LocateApplication(appSpec.fCreator, theFileSpec))
  319.             {
  320.                 *creator = appSpec.fCreator;
  321.                 return noErr;
  322.             }
  323.         }
  324.     }
  325.  
  326.     return -1;
  327. }
  328.  
  329. /***********************************************************************\
  330.     LocateApplication -- find the latest version of the specified
  331.         application on the user's disks
  332.     By Tom Emerson
  333. \***********************************************************************/
  334.  
  335. Boolean CInternetConfig::LocateApplication(OSType signature, FSSpec *theFileSpec)
  336. {
  337.     short    index = 0;
  338.     Boolean    found = false;
  339.     
  340.     HVolumeParam    hpb;
  341.     DTPBRec            dpb;
  342.     HFileInfo        cpb;
  343.     
  344.     FSSpec            most_recent;
  345.     unsigned long    most_recent_date = 0;
  346.     
  347.     Str63            app_name;
  348.     
  349.     do
  350.     {
  351.         ClearMem(&hpb, sizeof(HVolumeParam));
  352.         ClearMem(&dpb, sizeof(DTPBRec));
  353.         
  354.         hpb.ioVolIndex = ++index;
  355.         if (PBHGetVInfoSync((HParamBlockRec *)&hpb) != noErr)
  356.             break;
  357.         
  358.         dpb.ioVRefNum = hpb.ioVRefNum;
  359.         if (PBDTGetPath(&dpb) == noErr)
  360.         {
  361.             dpb.ioFileCreator = signature;
  362.             dpb.ioNamePtr = app_name;
  363.             
  364.             if (PBDTGetAPPLSync(&dpb) == noErr)
  365.             {
  366.                 ClearMem(&cpb, sizeof(HFileInfo));
  367.                 cpb.ioNamePtr = app_name;
  368.                 cpb.ioVRefNum = dpb.ioVRefNum;
  369.                 cpb.ioDirID = dpb.ioAPPLParID;
  370.                 
  371.                 if ((PBGetCatInfoSync((CInfoPBRec *)&cpb) == noErr) &&
  372.                     (cpb.ioFlMdDat > most_recent_date))
  373.                 {
  374.                     most_recent.vRefNum = dpb.ioVRefNum;
  375.                     most_recent.parID = dpb.ioAPPLParID;
  376.                     BlockMoveData(app_name, most_recent.name, 64);
  377.                     most_recent_date = cpb.ioFlMdDat;
  378.                 }
  379.                 
  380.                 found = true;
  381.             }
  382.         }
  383.     } while (! hpb.ioResult);
  384.     
  385.     if (found)
  386.         *theFileSpec = most_recent;
  387.         
  388.     return found;
  389. }
  390.  
  391. /***********************************************************************\
  392.     ClearMem - clear a chunk of memory
  393. \***********************************************************************/
  394.  
  395. void ClearMem(void *start, long len)
  396. {
  397.     char *p = (char *)start;
  398.     
  399.     for(long i=0; i<len; i++)
  400.         p[i] = 0;
  401. }
  402.